package com.across.connect.home;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.jdom.JDOMException;

import com.across.connect.dao.UserXmlParser;
import com.across.connect.model.MyUser;
import com.across.connect.util.EmailHashUtil;
import com.across.connect.util.XnConnect;
import com.xiaonei.api.schema.User;
import com.xiaonei.api.schema.UserAccount;

public class MyUserHome {

	private static MyUserHome instance = new MyUserHome();

	public static MyUserHome getInstance() {
		return instance;
	}

	public boolean isHaveEmail(final String email) {
		try {
			return UserXmlParser.isHaveEmail(email, EmailHashUtil.getEmailHash(email));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean isHaveUserName(final String username) {
		try {
			return UserXmlParser.isHaveUserName(username);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	public MyUser getMyUserByPassport(final String username, final String pwd) {
		try {
			return UserXmlParser.getPerson(username, pwd);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public MyUser getMyUserByUserID(final int userid) {
		try {
			return UserXmlParser.getPersonByUserid(userid);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public MyUser createUser(final String username, final String password,
			final String email, final String name) {
		try {
			MyUser user = new MyUser(username, password, name, email);
			user.setId(UserXmlParser.addPersonXml(user));
			//向校内注册新用户的Email信息
			UserAccount account = new UserAccount();
			account.setEmailHash(EmailHashUtil.getEmailHash(email));
			account.setUserSiteID(String.valueOf(user.getId()));
			XnConnect.xiaonei_registerUser(account);
			return user;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public MyUser createFromXiaoneiUID(int uid) {
		// first, check if there's a local account we should be stealing
		MyUser myuser = getMyUserByXiaoneiUID(uid);
		if (myuser != null) {
			return myuser;
		}
		
		User xnUser = XnConnect.getXiaoneiUserInfo(uid, uid);
		if (xnUser == null) {
			return null;
		}
		
		myuser = getMyUserByXiaoneiEmailHashes(xnUser.getEmailHash());
		// if we found a user, just add a uid and userName; otherwise create a new one
		if (myuser != null) {
			if (myuser.getXnuid() != uid) {
				myuser.setXnuid(uid);
				myuser.setName(xnUser.getName());
				try {
					UserXmlParser.updateMyUser(myuser);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return myuser;
		} else {
	      // We could allow the user to choose their own username if we want to
	      // In this site, the username doesn't show up anywhere, so there's not much
	      // point, but for a site where the username is a publicly-visible concept,
	      // you may want to allow them to choose one.
			myuser = new MyUser();
			myuser.setEmailHash(xnUser.getEmailHash());
			myuser.setUserName("XiaoneiUser_" + uid);
			myuser.setXnuid(uid);
			myuser.setName(xnUser.getName());
	      // Don't add a password for the user. The whole point is that they log in
	      // via Facebook. You could choose to let them add a password if you want
	      // to give them an alternate means of login, but it's not recommended.
			try {
				myuser.setId(UserXmlParser.addPersonXml(myuser));
				return myuser;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (JDOMException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getXiaoneiUserEmailHashes(int uid) {
		try {
			return XnConnect.getXiaoneiUserEmailHashes(uid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public MyUser getMyUserByXiaoneiEmailHashes(String emailhash) {
		try {
			if (StringUtils.isBlank(emailhash)) {
				return null;
			}
			return UserXmlParser.getMyUserByEmailHash(emailhash);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public MyUser getMyUserByXiaoneiUID(int uid){
		try {
			if (uid < 1) {
				return null;
			}
			return UserXmlParser.getPersonByXiaoneiUID(uid);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
